<template>
  <div class="box">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
      <el-form-item label="预警时间" prop="dateRange">
        <el-date-picker
          v-model.trim="dateRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预警级别">
        <el-select
          v-model.trim="queryParams.parameter.level"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in levelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select
          v-model.trim="queryParams.parameter.readStatus"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option label="已读" value="1"></el-option>
          <el-option label="未读" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="内容">
        <el-input
          v-model.trim="queryParams.parameter.stcd"
          placeholder="内容"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native.prevent="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="warnList" border @row-dblclick="dbClick">
      <el-table-column :resizable="false" label="序号" align="center" type="index" width="80"/>
      <el-table-column :resizable="false" label="预警时间" align="center" prop="warningTime" width="180"/>
      <el-table-column :resizable="false" label="预警级别" align="center" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.level == 1">一级预警</span>
          <span v-else-if="scope.row.level == 2">二级预警</span>
          <span v-else-if="scope.row.level == 3">三级预警</span>
          <span v-else-if="scope.row.level == 4">四级预警</span>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="标题" align="center" prop="warnType"></el-table-column>
      <el-table-column :resizable="false" label="内容" align="center" prop="warningExplain"></el-table-column>
      <el-table-column :resizable="false" label="是否已读" align="center" width="180">
        <template slot-scope="scope">
          <span 
            :class="scope.row.readStatus == 0 ? 'red' : ''" 
            style="text-decoration: underline;cursor: pointer;"
            @click="handleDeal(scope.row)"
          >{{scope.row.readStatusName}}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="75%" append-to-body :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="dealType == 'View'">
        <el-row>
          <el-col :span="24">
            <el-form-item label="信息标题" prop="title">
              <div class="border">{{form.title}}</div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="信息类型" prop="infoType">
              <div class="border">{{form.infoType}}</div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容摘要" prop="intro">
              <div class="border">{{form.intro}}</div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="信息内容">
              <div class="border" v-html="form.content">{{form.intro}}</div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listWarn, readWarn } from "@/api/system/user";

export default {
  name: "Notice",
  data() {
    return {
      // 上传图片的infoId
      infoId: '',
      // 上传图片的列表
      files: [],
      // 富文本编辑器
      editor: undefined,
      // 任务状态选项
      taskStatusOptions: [],
      // 权限列表
      permiList: [],
      // 权限类型
      dealType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 组中数组的标题列表
      titles: [],
      // 选中行
      selectRows:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      warnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      levelOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 50,
        parameter: {
          warningBeginTime: undefined,
          warningEndTime: undefined,
          readStatus: undefined,
          stcd: undefined,
          level: undefined,
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "公告标题不能为空", trigger: "blur" }
        ],
        infoType: [
          { required: true, message: "公告类型不能为空", trigger: "change" }
        ],
        intro: [
          { required: true, message: "内容摘要不能为空", trigger: "change" }
        ]
      }
    };
  },
  watch: {
    open(){
      if(!this.open){
        this.infoId = '';
      }
    }
  },
  activated() {
    this.getList();
    this.getDicts('WARNING_INFO_LEVEL').then(response=>{
      if(response.status == 200){
        this.levelOptions = response.data;
      }
    })
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      this.queryParams.parameter.warningBeginTime = this.dateRange && this.dateRange.length ? this.$utils.dateTimeFormat(this.dateRange[0]) : undefined;
      this.queryParams.parameter.warningEndTime = this.dateRange && this.dateRange.length ? this.$utils.dateTimeFormat(this.dateRange[1]) : undefined;
      listWarn(this.queryParams).then(res => {
        if(res.status == 200){
          this.warnList = res.data.list;
          this.total = res.data.total;
        }else{
          this.warnList = [];
          this.total = 0;
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        title: undefined,
        infoType: '1',
        intro: undefined,
        content: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageIndex: 1,
        pageSize: 50,
        parameter: {
          infoType: undefined,  
          beginTime: undefined,  
          endTime: undefined,
          title: undefined
        }
      }
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 权限按钮操作 */
    handleDeal(row){
      this.dealType = 'View';
      this.handleView(row);
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      this.form = {...row}
      this.dealType = 'View';
      if(row.readStatus == 0){
        this.readInfo(this.form.id);
      }
      // this.open = true;
      // this.title = "查看信息";
    },
    // 当行双击事件
    dbClick(row,column){
      this.handleDeal(row);
    },
    // 阅读信息
    readInfo(id) {
      let param = {
        parameter: {
          id: id
        }
      }
      readWarn(param).then((res)=>{
        if(res.status == 200){
          this.getList();
        }
      })
    }
  }
};
</script>   
<style lang="scss" scoped>
.border{
  border: 1px solid #dfe6ec;
  border-radius: 5px;
  padding-left: 12px;
}
.red{
  color:#ff4949
}
</style>